package com.zx._09_架构.rpc演变过程.rpc_02;

import com.zx._09_架构.rpc演变过程.service.UserService;
import com.zx.common.entity.UserRpcDto;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

// 优化1：Stub 是 consumer 端的代理对象，负责网络的连接、请求数据发送、响应接受、数据封装等
public class Stub implements UserService {

    @Override
    public UserRpcDto getById(final long queryId) {
        final Socket socket;
        try {
            socket = new Socket("localhost", 8888);
            final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            final DataOutputStream output = new DataOutputStream(outputStream);
            output.writeLong(queryId);

            final OutputStream socketOut = socket.getOutputStream();
            socketOut.write(outputStream.toByteArray());
            socketOut.flush();

            final InputStream inputStream = socket.getInputStream();
            final DataInputStream input = new DataInputStream(inputStream);
            final long id = input.readLong();
            final String name = input.readUTF();
            final UserRpcDto userRpcDto = new UserRpcDto(id, name);
            outputStream.close();
            socket.close();
            return userRpcDto;
        } catch (final IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
